@echo off

@set g_exe_dir_path=%~1
@set g_bat_dir_path=%~2
@set g_obj_dir_path=%~3
@set g_config_file_path=%~4

@if not defined g_exe_dir_path (
    echo Error: not set exe dir path
	exit /B 1
)

@if not defined g_bat_dir_path (
    echo Error: not set bat dir path
	exit /B 1
)

@if not defined g_obj_dir_path (
    echo Error: not set obj dir path
	exit /B 1
)

@if not defined g_config_file_path (
    echo Error: not set config file path
	exit /B 1
)

@set curr_dir_path=%cd%

call %g_bat_dir_path%\readini.bat %g_config_file_path% file hexFilePath hexFilePath 
call %g_bat_dir_path%\readini.bat %g_config_file_path% file hexFileName hexFileName

@if not defined hexFilePath (
	@echo Error: .ini file [file: hexFilePath] does not set param
	exit /B 1
)

@if not defined hexFileName (
	@echo Error: .ini file [file: hexFileName] does not set param
	exit /B 1
)

@set g_curr_use_file=%hexFileName%

@echo hex file name: %hexFileName%

@copy %hexFilePath%\%g_curr_use_file%.hex %g_obj_dir_path%\

call %g_bat_dir_path%\crop_hex_file.bat %g_curr_use_file% g_curr_use_file
call %g_bat_dir_path%\merge_hex_file.bat %g_curr_use_file% g_curr_use_file
call %g_bat_dir_path%\add_version.bat %g_curr_use_file% g_curr_use_file strVersion
call %g_bat_dir_path%\add_git_commit.bat %g_curr_use_file% g_curr_use_file
call %g_bat_dir_path%\generate_bin.bat %g_curr_use_file%

@if not %errorlevel% equ 0 (
    echo Error: exit
	exit /B 1
)

copy %g_obj_dir_path%\%g_curr_use_file%.* %g_obj_dir_path%\%strVersion%.*

call :output
call :clear
exit /B 0



@REM 输出文件到指定路径
:output
call %g_bat_dir_path%\readini.bat %g_config_file_path% file outputPath outputPath 

@if not defined outputPath (
	@echo Warning: .ini file [file: outputPath] does not set param
	exit /B 0
)

@if exist %outputPath% (
	@del /q %outputPath%\*.*
	@rd %outputPath%
)

@md %outputPath%

copy %g_obj_dir_path%\%strVersion%* %outputPath%

exit /B 0


@REM 清除临时文件
:clear
call %g_bat_dir_path%\readini.bat %g_config_file_path% option isClearTmpFile isClearTmpFile 

@if not defined isClearTmpFile (
	@echo Warning: .ini file [option: isClearTmpFile] does not set param
	exit /B 0
)

@if %isClearTmpFile% equ 0 (
	@exit /B 0
)

@del /q %g_obj_dir_path%\*.*

exit /B 0